//
//  HMMessageTableViewController.swift
//  sinaweibo
//
//  Created by lst on 16/5/19.
//  Copyright © 2016年 lst. All rights reserved.
//

import UIKit

class HMMessageTableViewController: HMBaseTableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        visitorLoginView.updateUI("登录后，别人评论你的微博，发给你的消息，都会在这里收到通知", imageName: "visitordiscover_image_message")
        
        
        tabBarItem.badgeValue = "\(10)"
        
        setBackground()
    }
    
    
    //设置背景图片
    private func setBackground() {
        for subview in self.tabBarController!.tabBar.subviews{
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                for v in subview.subviews {
                    if v.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                        for bgView in v.subviews {
                            if bgView.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                print(bgView,"终于找到你,还好没放弃")
                                
                                //设置背景图片 但是需要知道到底应该设置谁
                                //outCount: 表示数组的数量
                                var count: UInt32 = 0
                                
                                let list = class_copyIvarList(NSClassFromString("_UIBadgeBackground")!.self, &count)
                                //遍历成员变量 获取成员变量的名称
                                //c语言的数组 不支持快速遍历
                                print(count)
                                for i in 0..<count {
                                    //获取成员变量指针
                                    let ivarPoint = list[Int(i)]
                                    print(ivarPoint)
                                    //将成员变量的内存地址转换为 字符串
                                    //c 语言下的字符串
                                    let ivarName = ivar_getName(ivarPoint)
                                    print(ivarName)
                                    //将c语言字符串转换 String
                                    let name = String(format: "%s", ivarName)
                                    let type = ivar_getTypeEncoding(ivarPoint)
                                    let typeName = String(format: "%s", type)
                                    print(name,typeName)
                                    let image = UIImage(named: "main_badge")
                                    //调用 _UIBadgeBackground类型的对象的KVC方法 简介设置值
                                    bgView.setValue(image, forKey: "_image")
                                    bgView.setValue(image, forKey: "_image")
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
}
